package com.siwei.mes.controller.experiment;

import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.validator.AddGroup;
import com.siwei.mes.common.validator.PageGroup;
import com.siwei.mes.common.validator.UpdateGroup;
import com.siwei.mes.entity.experiment.ExperimentSupplyTask;
import com.siwei.mes.service.experiment.ExperimentSupplyTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 试验台账与供货任务关系相关
 *
 * @author makejava
 * @since 2024-05-28 23:00:03
 */
@Api(value = "试验台账与供货任务关系")
@RestController
@RequestMapping("experimentSupplyTask")
public class ExperimentSupplyTaskController {
    @Resource
    private ExperimentSupplyTaskService experimentSupplyTaskService;

    @ApiOperation(value = "根据试验台账id获取供货任务单信息")
    @GetMapping("selectByExperimentId")
    public Json selectByExperimentId(@RequestParam("experimentId") Long experimentId) {
        return Json.succ().data(experimentSupplyTaskService.selectByExperimentId(experimentId));
    }

    @ApiOperation(value = "新增试验台账与供货任务关系")
    @PostMapping("/create")
    public Json create(@RequestBody @Validated(AddGroup.class) ExperimentSupplyTask entity) {
        return Json.succ().data(ApiResult.addColumn("id", experimentSupplyTaskService.create(entity)));
    }

    @ApiOperation(value = "修改试验台账与供货任务关系")
    @PostMapping("/update")
    public Json update(@RequestBody @Validated(UpdateGroup.class) ExperimentSupplyTask entity) {
        experimentSupplyTaskService.update(entity);
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "删除试验台账与供货任务关系")
    @PostMapping("/delete")
    public Json delete(@RequestBody ExperimentSupplyTask entity) {
        experimentSupplyTaskService.delete(entity.getId());
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "分页获取试验台账与供货任务关系")
    @PostMapping("/page")
    public Json appPage(@RequestBody @Validated(PageGroup.class) PageQuery<ExperimentSupplyTask, ExperimentSupplyTask> query) {
        return Json.succ().data(experimentSupplyTaskService.getPage(query));
    }

    @ApiOperation(value = "根据主键id获取试验台账与供货任务关系")
    @GetMapping("loadById")
    public Json loadById(@RequestParam("id") Long id) {
        return Json.succ().data(experimentSupplyTaskService.loadById(id));
    }

    @ApiOperation(value = "获取所有试验台账与供货任务关系")
    @PostMapping("getAll")
    public Json getAll(@RequestBody ExperimentSupplyTask entity) {
        return Json.succ().data(ApiResult.addList(experimentSupplyTaskService.getAll(entity)));
    }

    @ApiOperation(value = "根据试验台账id和任务单号 删除对应关联关系")
    @PostMapping("deleteByExperimentIdAndSupplyTaskId")
    public Json deleteByExperimentIdAndSupplyTaskId(@RequestBody ExperimentSupplyTask entity) {
        Long experimentId = entity.getExperimentId();//试验委托id
        Long taskId = entity.getSupplyTaskId();//任务id
        assertTrue(experimentId == null, "试验委托id为空");
        assertTrue(taskId == null, "任务id为空");
        experimentSupplyTaskService.deleteByExperimentIdAndSupplyTaskId(experimentId, taskId);
        return Json.succ().data(new ApiResult());
    }
}
